home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / linux_bo / netboot.zoo / udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-29  |  3.6 KB  |  150 lines

  1. /*
  2.  *    UDP.C
  3.  *    UDP Protocol Routines
  4.  *
  5.  *    Revision history:
  6.  *    JWH Apr 93 - substantial changes
  7.  *
  8.  *    This was originally part of NCSA Telnet but
  9.  *    has undergone substantial modification     
  10.  */
  11.  
  12. #include "protocol.h"
  13. #include "bootinc.h"
  14.  
  15. void 
  16. dldump(DLAYER * p)
  17. {
  18.     n_printf("dest=%x.%x.%x.%x.%x.%x  me=%x.%x.%x.%x.%x.%x type %d\n",
  19.            p->dest[0], p->dest[1], p->dest[2], p->dest[3], p->dest[4], p->dest[5],
  20.            p->me[0], p->me[1], p->me[2], p->me[3], p->me[4], p->me[5],
  21.            p->type);
  22. }
  23.  
  24.  
  25. void 
  26. udpdump(UDPKT * p)
  27. {
  28.     n_printf("Dest port %d, source port %d length %d\n", intswap(p->u.dest), intswap(p->u.source), intswap(p->u.length));
  29. }
  30.  
  31. /*
  32.  *    udprecv ( p, port )
  33.  *
  34.  *    Take an incoming UDP packet and make the available to the user level
  35.  * routines.  Currently keeps the last packet coming in to a port.
  36.  *
  37.  *    Limitations :
  38.  *
  39.  * Can only listen to one UDP port at a time, only saves the last packet
  40.  * received on that port.  Port numbers should be assigned like TCP ports.
  41.  *
  42.  * returns length if packet available, 0 if not
  43.  */
  44. int 
  45. udprecv(p, uport)
  46. UDPKT          *p;
  47. uint            uport;
  48. {
  49.     uint            hischeck,
  50.                     mycheck;
  51.     int             ulen;
  52.     int         code;
  53.  
  54.     ulen = iprecv((IPKT *) p);
  55.     if (!ulen)
  56.         return 0;
  57.     if (ulen < 0)
  58.         return ulen;
  59.     if (debug)
  60.         dldump((DLAYER *) p);
  61. /*
  62.  *  did we want this data ?  If not, then let it go, no comment
  63.  *  If we want it, copy the relevent information into our structure
  64.  */
  65.     if (intswap(p->u.dest) != uport)
  66.         return (0);
  67. /*
  68.  *  first compute the checksum to see if it is a valid packet
  69.  */
  70.     hischeck = p->u.check;
  71.     p->u.check = 0;
  72.     if (hischeck)
  73.     {
  74.         memcpy(tcps.source, p->i.ipsource, 8);
  75.         tcps.z = 0;
  76.         tcps.proto = p->i.protocol;
  77.         tcps.tcplen = intswap(ulen);
  78.         mycheck = tcpcheck((char *) &tcps, (char *) &p->u, ulen);
  79.         if (hischeck != mycheck)
  80.         {
  81.             netposterr(700);
  82.             return (0);
  83.         }
  84.         p->u.check = hischeck;    /* put it back */
  85.     }
  86.     ulen -= 8;    /* account for header */
  87.     if (ulen > UMAXLEN)    /* most data that we can accept */
  88.         ulen = UMAXLEN;
  89.     return (ulen);
  90. }
  91.  
  92. /*
  93.  *    netusend ( machine, eaddr, port, retport, buffer, n )
  94.  *
  95.  *    Send some data out in a udp packet ( uses the preinitialized data in the
  96.  * port packet *ulist.udpout* )
  97.  *
  98.  *    Returns 0 on ok send, non-zero for an error
  99.  *
  100.  */
  101.  
  102. int 
  103. netusend(machine, eaddr, port, retport, buffer, n)
  104. unsigned char  *machine,
  105.                *buffer,
  106.                *eaddr;
  107. uint            port,
  108.                 retport;
  109. int             n;
  110. {
  111.  
  112.     if (n > UMAXLEN)
  113.         n = UMAXLEN;
  114. /*
  115.  *  make sure that we have the right dlayer address
  116.  */
  117.     memcpy(udpout.d.me, nnmyaddr, DADDLEN);
  118.     memcpy(udpout.d.dest, eaddr, DADDLEN);
  119.     memcpy(udpout.i.ipdest, machine, 4);
  120.     memcpy(utcps.dest, machine, 4);
  121.     udpout.u.dest = intswap(port);
  122.     udpout.u.source = intswap(retport);
  123.     utcps.tcplen = udpout.u.length = intswap(n + sizeof(UDPLAYER));
  124.     memcpy(udpout.data, buffer, n);
  125. /*
  126.  *  put in checksum
  127.  */
  128.     udpout.u.check = 0;
  129.     udpout.u.check = tcpcheck((char *) &utcps, (char *) &udpout.u, n + sizeof(UDPLAYER));
  130. /*
  131.  *   iplayer for send
  132.  */
  133.     udpout.i.tlen = intswap(n + sizeof(IPLAYER) + sizeof(UDPLAYER));
  134.     udpout.i.ident = intswap(nnipident++);
  135.     udpout.i.check = 0;
  136.     udpout.i.check = ipcheck((char *) &udpout.i, 10);
  137. /*
  138.  *  send it
  139.  */
  140.     if (debug)
  141.     {
  142.         n_printf("Sending....\n");
  143.         dldump((DLAYER *) & udpout);
  144.         ipdump((IPKT *) & udpout);
  145.         udpdump(&udpout);
  146.     }
  147.  
  148.     return (dlayersend((DLAYER *) & udpout, sizeof(DLAYER) + sizeof(IPLAYER) + sizeof(UDPLAYER) + n));
  149. }
  150.